package cn.hj.user.controller;


import cn.hj.basic.dto.UserDto;
import cn.hj.basic.exception.BusinessException;
import cn.hj.basic.util.AjaxResult;
import cn.hj.basic.util.PageList;
import cn.hj.user.domain.User;
import cn.hj.user.query.UserQuery;
import cn.hj.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Api(tags = "用户接口",description = "用户接口详细描述")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 根据Id查询数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询",notes = "根据id查询")
    public User getById(@PathVariable("id") Long id){
        return  userService.findById(id);
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/index")
    @ApiOperation(value = "查询所有",notes = "")
    public List<User> getAll(){
        return userService.findAll();
    }

    /**
     * 分页查询加高级查询
     * @param query
     * @return
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页查询和高级查询",notes = "")
    public PageList<User> queryData(@RequestBody UserQuery query){
       return  userService.selectData(query);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            userService.batchRemove(ids);
            return AjaxResult.newAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.newAjaxResult().setMessage("删除失败").setSuccess(false);
        }
    }

    /**
     * 根据id删除数据
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除",notes = "根据id删除")
    public AjaxResult remove(@PathVariable("id") Long id) {
        try {
            userService.delete(id);
            return AjaxResult.newAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.newAjaxResult().setSuccess(false).setMessage("操作失败");
        }
    }

    /**
     *新增或修改
     * @param user
     * @return
     */
    @ApiOperation(value = "用户添加或修改",notes = "如果有id就是修改否则就是添加")
    @PostMapping("/save")
    public  AjaxResult save(@RequestBody User user){
        try {

            //如果id不在存执行新增，不存在执行修改
            if (user.getId() ==null){
                userService.insert(user);

            }else {
                userService.update(user);
            }
            return  AjaxResult.newAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return   AjaxResult.newAjaxResult().setSuccess(false).setMessage("操作失败");
        }
    }

    /**
     * 手机注册
     * @param dto
     * @return
     */
    @PostMapping("/register/phone")
    public AjaxResult registerPhone(@RequestBody UserDto dto){
        try {
            userService.registerPhone(dto);
            return AjaxResult.newAjaxResult();
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.newAjaxResult().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e){
            return AjaxResult.newAjaxResult().setSuccess(false).setMessage("系统错误，正在殴打程序员");
        }
    }
}
